$(document).ready(function(){
    var currentpage=0;
    getData();
	  function getData(){
		  $.getJSON("/job/possition/listAllById", { start: currentpage}, function(json){
			  console.log("************1****"+currentpage);
		  		var contentdata=json.data.content;
		  	  $("#databody").empty();
		       for(var i=0;i<contentdata.length;i++){
		    	   $("#databody").append(" <tr>");
						$("#databody").append(" <td>"+contentdata[i].name+"</td>");
						$("#databody").append(" <td>"+contentdata[i].detail+"</td>");
						$("#databody").append(" <td>"+contentdata[i].education+"</td>");
						$("#databody").append(" <td><button type='button' class='btn btn-primary'>查看详情</button>&nbsp&nbsp&nbsp<button type='button' class='btn btn-primary' name='trdow' id='dowbtn"+contentdata[i].id+"'>简历管理</button>&nbsp&nbsp&nbsp<button type='button' name='trupdate' id='updatebtn"+contentdata[i].id+"' class='btn btn-primary'>修改</button>&nbsp&nbsp&nbsp<button  type='button' name='trdelete' class='btn btn-danger' id='deletebtn"+contentdata[i].id+"'>删除</button></td>");
		    	   $("#databody").append(" </tr>");
		       }

		       //动态设置分页
			 var totalPagesnumber=json.data.totalPages;
			  $(".pagination").empty();
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="firstpage">首页</a></li>');
			  $(".pagination").append('<li class=""><a class="page-link" href="#" id="previosepage">上一页</a></li>');
		       for(var j=0;j<totalPagesnumber;j++){
		    	   $(".pagination").append(' <li class="page-item" id="pageno'+j+'"><a class="page-link" href="#">'+(j+1)+'</a></li>');
		       }
		       $(".pagination").append(' <li class=""><a class="page-link" href="#" id="nextpage">下一页</a></li>');
			//把当前页，颜色变成蓝色
				$(".page-item").removeClass("active");
				$("#pageno"+currentpage).addClass("active");

				    $(".page-item").click(function(){

					  var idval=this.id;
					  currentpage=idval.substr(6);

					  getData();
				  })


				  //下一页
			  $("#nextpage").click(function(){
				  var  curr= new Number(currentpage);
				  currentpage=curr+1;
				  getData();
			  })


			  //上一页
			   $("#previosepage").click(function(){
				   var  curr= new Number(currentpage);
				  currentpage=curr-1;
				  getData();
			  })

			   //首页
			   $("#firstpage").click(function(){
				  currentpage=0;
				  getData();
			  })

			  //修改功能1
              $("button[name='trupdate']").click( function () {
                        let idval=$(this).attr("id");
                        $("input[name='updatetid']").val(idval.substr(9));
              	        $('#updateModal').modal('show')
              });
              //修改功能1 结束

			  //删除功能
                    $("button[name='trdelete']").click( function () {
                             var r=confirm("确认删除吗？");
                              if (r==true){
                                          let idval=$(this).attr("id");
                                          console.log(idval);
                                          console.log(idval.substr(9));
                                          $.ajax({
                                                      url : "/job/possition/delete/"+idval.substr(9),
                                                      async : false,
                                                      type : "POST",
                                                      contentType : 'application/json',
                                                      dataType : 'json',

                                                      success : function(data) {
                                                          alert("删除成功");
                                                          window.location.href="/job/personalpossition.html";
                                                      }
                                                  });
                              }
                              else{
                                  console.log("取消了");
                              }
                    });
                     //删除功能 结束

                     //简历管理功能
                      $("button[name='trdow']").click( function () {
                        dowId=$(this).attr("id").substr(6);
                          oepndow(dowId);
                        });
                      //简历管理结束

		});

	  }

    $("#addbtn").click(function(){
        var data =  {"name" :$("#nameput").val(),"city" :$("#cityput").val(),"detail" :$("#detailput").val(),"education" :$("#educationput").val(),"maxSales" :$("#maxSalesput").val(),"minSales" :$("#minSalesput").val(),"requiredNum" :$("#requiredNumput").val(),"workedyears" :$("#workedyearsput").val(),"address" :$("addressput").val()}
        $.ajax({
            url : "/job/possition/register",
            async : false,
            type : "POST",
            contentType : 'application/json',
            dataType : 'json',
            data :JSON.stringify(data),
            success : function(data) {
               window.location.href="/job/personalpossition.html";
            }
        });
    })

        $("#updatebtn").click(function(){
            var data =  {"name" :$("#nameput2").val(),"city" :$("#cityput2").val(),"detail" :$("#detailput2").val(),"education" :$("#educationput2").val(),"maxSales" :$("#maxSalesput2").val(),"minSales" :$("#minSalesput2").val(),"requiredNum" :$("#requiredNumput2").val(),"workedyears" :$("#workedyearsput2").val(),"address" :$("addressput2").val()}
            $.ajax({
                url : "/job/possition/register",
                async : false,
                type : "POST",
                contentType : 'application/json',
                dataType : 'json',
                data :JSON.stringify(data),
                success : function(data) {
                   window.location.href="/job/personalpossition.html";
                }
            });
        })

    function oepndow(dowId){
         console.log(dowId);
         getData2();
         function getData2(){
            $.getJSON("/job/possition/showById/"+dowId, function(json){
                	var contentdata=json.data.resumeList;
                    $("#dowbody").empty();
      		       for(var i=0;i<contentdata.length;i++){
      		    	   $("#dowbody").append(" <tr>");
                            $("#dowbody").append("<td>"+contentdata[i].name+"</td>");
      						$("#dowbody").append(" <td>"+contentdata[i].createDate+"</td>");
      						$("#dowbody").append(" <td><a href='"+contentdata[i].address+"'>下载</a></td>");
      		    	   $("#databody").append(" </tr>");
      		       }
                    $('#dowModal').modal("show")
            })

         }
    }

	})
